<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index:1201;">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="12">
          <el-form-item label="凭证字" prop="busMainData.voucherPrefix">
            <el-select v-model="form.busMainData.voucherPrefix" :style="style" size="mini" filterable placeholder="凭证字">
              <el-option
                v-for="(item) in dicts.voucher_prefix"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"/>
            </el-select>
          </el-form-item>
          <el-form-item label="业务日期" prop="busMainData.voucherDate">
            <el-date-picker
              v-model="form.busMainData.voucherDate"
              placeholder="业务日期"
              type="date"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属账期" prop="busMainData.voucherMonth">
            <el-input v-model="form.busMainData.voucherMonth" disabled />
          </el-form-item>
          <el-form-item label="附档" prop="attachmentList">
            <FileUpload
              ref="fileUpload"
              :editable="form.busPageInfo.editable"
              :sup_this="sup_this"
              :file-limit="5"
              :is-direct-upload="true"
              list-type="list"
              @fileRemoveSuccess="fileRemoveSuccess"
              @fileUploadSuccess="fileUploadSuccess"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-collapse v-model="activeNames">
      <el-collapse-item title="凭证明细" name="1">
        <voucher-detail ref="voucherDetail" :bus-main-data="form.busMainData" :voucher-details="form.voucherDetails" :editable="form.busPageInfo.editable" @setDetails="setDetails" />
      </el-collapse-item>
    </el-collapse>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','FIN_VOUCHER_ALL','FIN_VOUCHER_CREATE']" :loading="loading" type="primary" @click="doSave">保存</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import finVoucher from '@/api/fin/FinVoucher'
import VoucherDetail from './voucherDetail'

export default {
  dictTypes: ['voucher_status', 'voucher_prefix'],
  components: { VoucherDetail },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      activeNames: ['1'],
      isAdd: false,
      loading: false,
      style: 'width: 184px',
      dialogInfo: {
        name: 'finVoucherMngFormDialog',
        title: '记帐凭证管理',
        fullScreen: false,
        width: 80,
        height: 90
      },
      form: {
        voucherDetails: [],
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.voucherPrefix': [
          { required: true, message: '请输入凭证字', trigger: 'blur' }
        ],
        'busMainData.voucherDate': [
          { required: true, message: '请输入业务日期', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    fileRemoveSuccess(file) {
      this.form.attachmentList = this.form.attachmentList.filter(t => t.filePath !== file.filePath)
    },
    fileUploadSuccess(sysAttachment) {
      this.form.attachmentList.push(sysAttachment)
    },
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    setDetails(details) {
      this.form.voucherDetails = details
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        finVoucher.getDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.voucherDetails = res.data
        })
      }
      setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 200)
    },
    addDataFromSettleMonth(baseDto) {
      this.isAdd = true
      this.displayDialog()
      this.form = baseDto
      this.getOtherDetailData(null, true, true)
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      finVoucher.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(null, true, true)
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      finVoucher.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(mainId, true, true)
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      finVoucher.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.getOtherDetailData(id, false, false)
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      finVoucher.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      finVoucher.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    }
  }
}
</script>
